package com.whfp.coky.ui.task.vm.fluor

import android.content.Context
import android.widget.TextView
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer
import com.hjq.widget.view.SwitchButton
import com.whfp.coky.databinding.FragFluorSettingsBinding
import com.whfp.coky.other.Constants
import com.whfp.coky.other.DialogBuilder
import com.whfp.coky.ui.base.BaseActivity
import com.whfp.coky.ui.base.BaseFragment
import com.whfp.coky.ui.base.BaseViewModel
import com.whfp.coky.ui.task.act.FluorMainAct
import com.whfp.coky.ui.task.act.fluor.ContactUsAct
import com.whfp.coky.ui.task.frag.FluorSettingsFrag
import com.whfp.coky.ui.task.model.FluorSettingBean
import com.whfp.coky.utils.LogUtil

/**
 *
 *
 * created by coky on 2022/3/1
 */
class FluorSettingsFragVM: BaseViewModel<FragFluorSettingsBinding>() {

    var result = MutableLiveData<String>()
    var curveCurr = MutableLiveData<Int>()//当前标曲模式0,1,2
    var textTipsTrue:List<String> = listOf("覆盖","显示","快速","上位机","开启")
    var textTipsFalse:List<String> = listOf("不覆盖","不显示","标准","下位机","关闭")
    var textList:List<TextView>? = null

    override fun observe(fragment: BaseFragment<*, *>, owner: LifecycleOwner) {
        super.observe(fragment, owner)
        curveCurr.observe(fragment, Observer {
            LogUtil.e("curr.....${it}")
            if (it == -1||it == null){
                return@Observer
            }
            vBinding.sb03.setLeftText(Constants.ARRAY_CURVES[it].toString())
            (fragment.getAttachActivity() as FluorMainAct).getSettings()?.curveType = it
        })
    }

    /**
     * 加载设置界面，配置当前界面
     */
    fun doLoadSettings(frag: FluorSettingsFrag, bean: FluorSettingBean?){
        textList = listOf(vBinding.tvTips01,
            vBinding.tvTips02,
            vBinding.tvTips03,
            vBinding.tvTips04,vBinding.tvTips05)
        curveCurr.value = bean?.curveType
        changeSw(0,vBinding.sw01,bean?.isCoverOldProj)
        changeSw(1,vBinding.sw02,bean?.isShowResult)
        changeSw(2,vBinding.sw03,bean?.detecType)
        changeSw(3,vBinding.sw04,bean?.lowUpCaculate)
        changeSw(4,vBinding.sw05,bean?.isAlarm)
        vBinding.sw01.setOnCheckedChangeListener(frag)
        vBinding.sw02.setOnCheckedChangeListener(frag)
        vBinding.sw03.setOnCheckedChangeListener(frag)
        vBinding.sw04.setOnCheckedChangeListener(frag)
        vBinding.sw05.setOnCheckedChangeListener(frag)
    }

    //    加载按钮状态改变
    private fun changeSw(index:Int,sw: SwitchButton, flag:Boolean?){
        flag?.let {
            sw.setChecked(it)
            inputText(index,it)
        }
    }

    fun inputText(index:Int,isChecked:Boolean){
        textList!![index].text = if (isChecked)textTipsTrue[index]else textTipsFalse[index]
    }


    fun doShowCurveSelect(context: Context) {
//        LogUtil.e(curveSets.toString())
        LogUtil.e("curveCurr.value:${curveCurr.value!!}")
//        标曲选择方式
        DialogBuilder.selectSingle(context,curveCurr.value!!,
            "标曲选择方式设置",
            Constants.ARRAY_CURVES){
            curveCurr.value = it.keys.first()
        }
    }

    fun doDeviceAutoCheck() {
//        设备自检
    }

    fun doContactUs(activity:BaseActivity<*,*>) {
//        联系我们
        activity.startActivity(ContactUsAct::class.java)
    }


}